package com.example.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.Random;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import com.example.fw.ContactObject;
import com.example.fw.Contacts;
import com.example.fw.GroupObject;
import com.example.fw.Groups;

public class ContactModificationTests extends TestBase {

	Random rnd = new Random();
	private ContactObject contactAfterModification;
	
	@Test
	public void testSomeContactCanBeModificated() throws Exception {
		String month = ContactObject.MONTHS[new Random().nextInt(12)];
		Groups groups = app.getGroupHelper().getGroups();
		GroupObject someGroupFromList = groups.getSomeGroupFromList();
		ContactObject contactAfterModification = new ContactObject()
			.setName("Name"+rnd.nextInt())
			.setLastname("Lastname"+rnd.nextInt())
			.setAddress("Address"+rnd.nextInt())
			.setHomePhone(""+rnd.nextInt())
			.setMobilePhone(""+rnd.nextInt())
			.setWorkPhone(""+rnd.nextInt())
			.setEmail("email"+rnd.nextInt()+"@google.com")
			.setEmail2("email2"+rnd.nextInt()+"@google.com")
			.setBirthDay(""+app.getContactHelper().generateRandomNumberBetween(1, 31))
			.setBirthMonth(month)
			.setBirthYear(""+app.getContactHelper().generateRandomNumberBetween(1900, 2012))
			.setGroup(""+someGroupFromList.name)			
			.setAddress2("Address2-"+rnd.nextInt())
			.setPhone2(""+rnd.nextInt());
			
		Contacts oldContactList = app.getContactHelper().getContacts();
		ContactObject contactBeforeModification = oldContactList.takeSomeContactFromList();
		app.getContactHelper().modifyContact(contactBeforeModification, contactAfterModification);
		Contacts newContactList = app.getContactHelper().getContacts();
		assertThat(newContactList.withAdded(contactBeforeModification), equalTo(oldContactList.withAdded(contactAfterModification)));
		
		//modifiedContactId = contactAfterModification.getContactId();
	}
	
//	@AfterMethod		// lesson 7 ~ 75:00
//	public void testUiAndDbConformsWithModel()	{
//		if ((app.getProperty("ui.check") != null) && (app.getProperty("db.check") != null))	{
//			ContactObject contactFromUi = app.getContactHelper().getUiContactData(contactAfterModification);
//			ContactObject contactFromDb = app.getHibernateHelper().getDbContactData(contactAfterModification.getContactId());
//			assertThat(contactFromUi, equalTo(contactFromDb));
//		}
//	}
}